﻿
Imports std
Imports centrosphere
Imports command = centrosphere.job.operation.command
Imports lock_t = lock(Of locks.monitorlock)

Public Class operationJobConsumer
    Inherits jobConsumer(Of operationJob)

    Private Shared ReadOnly delegateDirectory As String = Nothing
    Private Shared ReadOnly outputDirectory = Nothing
    Private Shared ReadOnly processOutputDirectory = Nothing

    Shared Sub New()
        outputDirectory = fullpath(combinepath(filesystem.deployRoot, "operationJobConsumer"))
        createDirectory(outputDirectory)
        processOutputDirectory = fullpath(combinepath(outputDirectory, "processOutput"))
        createDirectory(processOutputDirectory)
        delegateDirectory = fullpath("delegates")
    End Sub

    Private ReadOnly jobs As map(Of String, job) = Nothing
    Private jobs_lock As lock_t

    Public Sub New()
        jobs = New map(Of String, job)()
        removeOldJobInfo()
    End Sub

    Private Sub lock_jobs()
        jobs_lock.wait()
    End Sub

    Private Sub unlock_jobs()
        jobs_lock.release()
    End Sub

    Private _reserveJobInfoMilliSeconds As Int64 = npos
    Private Property reserveJobInfoMilliseconds As Int64
        Get
            assert(_reserveJobInfoMilliSeconds <> npos)
            Return _reserveJobInfoMilliSeconds
        End Get
        Set(ByVal value As Int64)
            assert(_reserveJobInfoMilliSeconds = npos)
            assert(value <> npos AndAlso value > 0)
            _reserveJobInfoMilliSeconds = value
        End Set
    End Property

    Protected Overrides Function workOn(ByVal j As operationJob,
                                        Optional ByRef ec As eventComb = Nothing) As Boolean
        assert(Not j Is Nothing)
        Select Case j.command.as(Of command)()
            Case command.run
            Case command.result
            Case command.status
            Case Else
                raiseError(errorHandle.errorType.warning, "unknown command ", j.command, " got, ignore")
                Return False
        End Select

        Return True
    End Function
End Class
